<?php
namespace app\common\service;

class WXUtils {
	// 获取access_token
	public function getAccessToken() {
		require_once ROOT_PATH . 'wechatpay/lib/WxPay.Config.php';
		$wxconfig = new \WxPayConfig();
		$access_token_url = 'https://api.weixin.qq.com/cgi-bin/token';
		$access_token_param = [
			'grant_type' => 'client_credential',
			'appid' => $wxconfig::WX_APPID,
			'secret' => $wxconfig::APPSECRET,
		];
		$access_token_data = curlGet($access_token_url, $access_token_param);
		$access_token_json = json_decode($access_token_data, true);
		$access_token = $access_token_json['access_token'];
		return $access_token;
	}

	// 获取openid
	public function get_wechat_openid($code = '') {
		require_once ROOT_PATH . 'wechatpay/lib/WxPay.Config.php';
		// 1、首先根据code获取微信的openid
		$wxconfig = new \WxPayConfig();
		$url = 'https://api.weixin.qq.com/sns/jscode2session';
		$arr = [
			'appid' => $wxconfig::WX_APPID,
			'secret' => $wxconfig::APPSECRET,
			'js_code' => $code,
			'grant_type' => 'authorization_code',
		];
		$data = curlGet($url, $arr);
		$jsonData = json_decode($data, true);
		$openid = $jsonData && isset($jsonData['openid']) ? $jsonData['openid'] : false;
		return $openid;
	}

	// 发送微信模板消息
	public function sendTemplateMsg($openid, $templateData, $formid, $templateID) {
		// 1、获取access_token
		$accesstoken = $this->getAccessToken();
		// 2、调用接口
		$template_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $accesstoken;
		// $data_json = json_encode($data);
		$template_param = [
			'touser' => $openid,
			'template_id' => $templateID,
			'form_id' => $formid,
			'data' => $templateData,
			'page' => 'pages/index/index',
		];
		trace($template_param);
		$param_json = json_encode($template_param);
		$template_data = curlPost($template_url, $param_json);
		trace($template_data);
		//
		return;
	}

	// 充值荣耀会员，发送模板消息
	public function sendWxPayTemplateMsg($data) {
		// 发送支付成功的模板消息
		$openid = $data['openid'];
		$value1 = [
			'value' => $data['out_trade_no'],
		];
		$value2 = [
			'value' => round($data['total_fee'] / 100, 2) . '元',
		];
		$value3 = [
			'value' => $data['time_end'],
		];
		$value4 = [
			'value' => $data['attach'],
		];
		$template_data = [
			'keyword1' => $value1,
			'keyword2' => $value2,
			'keyword3' => $value3,
			'keyword4' => $value4,
		];
		trace($template_data);
		//
		$formid = model('WxpayInfo')->where('ordersn', $data['out_trade_no'])->value('prepayid');
		$templateID = 'HIJYIVEcA4qn7AAuzKPAwVeqowb3x_eROnE9zu5h1bQ';
		//
		$this->sendTemplateMsg($openid, $template_data, $formid, $templateID);
	}

	// 登录小程序，发送模板消息
	public function sendWxLoginTemplateMsg($data) {
		// 发送登录成功的模板消息
		$openid = $data['openid'];
		$value1 = [
			'value' => $data['realname'],
		];
		$value2 = [
			'value' => $data['logintime'],
		];
		$value3 = [
			'value' => $data['phone'],
		];
		$value4 = [
			'value' => $data['desc'],
		];
		$template_data = [
			'keyword1' => $value1,
			'keyword2' => $value2,
			'keyword3' => $value3,
			'keyword4' => $value4,
		];
		trace($template_data);
		//
		$formid = $data['formid'];
		$templateID = 'YlVrk3uuNUexwbJ9oe6hebkE3XBCQV0SwN1oWB-VYrY';
		//
		$this->sendTemplateMsg($openid, $template_data, $formid, $templateID);
	}

	// 注册账号，发送模板消息
	public function sendRegisterTemplateMsg($data) {
		// 发送登录成功的模板消息
		$openid = $data['openid'];
		$value1 = [
			'value' => $data['phone'],
		];
		$value2 = [
			'value' => $data['nickname'],
		];
		$value3 = [
			'value' => $data['addtime'],
		];
		$value4 = [
			'value' => $data['dealername'],
		];
		$template_data = [
			'keyword1' => $value1,
			'keyword2' => $value2,
			'keyword3' => $value3,
			'keyword4' => $value4,
		];
		trace($template_data);
		//
		$formid = $data['formid'];
		$templateID = 'bDA0ULJvRBtCzt8Mp0_5FPenRF8qAl2ygQzBZ9zwwHQ';
		//
		$this->sendTemplateMsg($openid, $template_data, $formid, $templateID);
	}

	// 线索成交，发送模板消息
	public function sendTradeTemplateMsg($data) {
		$openid = $data['openid'];
		// $openid = 'oF0s75H4JrHOcC6a0y1pfNz5GZ9c';
		$value1 = [
			'value' => $data['time'],
		];
		$value2 = [
			'value' => $data['name'],
		];
		$value3 = [
			'value' => $data['desc'],
		];
		$value4 = [
			'value' => $data['tip'],
		];
		$template_data = [
			'keyword1' => $value1,
			'keyword2' => $value2,
			'keyword3' => $value3,
			'keyword4' => $value4,
		];
		trace($data);
		//
		$formid = $data['formid'];
		$templateID = 'n6X5DIhf36OVxRG_IIeyxpZPo-0etppX6pGJ9bISW8k';
		//
		$this->sendTemplateMsg($openid, $template_data, $formid, $templateID);
	}
}
